<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>异步操作讲解</title>
    <link rel="stylesheet" href="hw/css/bootstrap.min.css">
</head>

<body>
    <div class="form-group has-error">
        <label class="control-label" for="inputSuccess1">Input with success</label>
        <input type="text" class="form-control" id="inputSuccess1" aria-describedby="helpBlock2">
        <span id="helpBlock2" class="help-block">A block of help text that breaks onto a new line and may extend beyond one line.</span>
    </div>
    <script>
        function getData(xx) {
            console.log('开始请求')//1
            var result = ''
            var xhr = new XMLHttpRequest()
            xhr.onreadystatechange = function (e) {
                if (xhr.readyState == 4) {
                    console.log('数据获取成功')//3
                    result = xhr.response
                    xx(result)
                }
            }
            xhr.open('get', 'http://localhost:3000/api/v1/all', true)
            xhr.send() //当请求是同步的时候,send之后会等待结果返回为止继续执行后面的代码
            console.log('请求的方法执行完毕')//2
            // return result
        }
        // var str = getData()
        // console.log(str)
        getData(function (str) {
            console.log(str)
        })
    </script>
</body>

</html>